import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_drawing_board/flutter_drawing_board.dart';
import 'package:flutter_drawing_board/paint_contents.dart';
import 'package:get/get.dart';

import '../../../sqflite/sql_utils.dart';
import '../../../utils/constant_utils.dart';
import '../../../utils/dialog_utils.dart';
import '../../../utils/enum_utils.dart';
import '../../../utils/logger_utils.dart';

/// 画板控制器
class DrawingBoardController extends GetxController {
  /// 图画控制器
  final DrawingController _drawingController = DrawingController();

  /// 当前选中的工具
  final tid = Icons.edit.obs;

  /// 当前的笔划宽度
  final strokeWidth = 4.0.obs;

  DrawingController getController() {
    return _drawingController;
  }

  /// 设置 笔划宽度
  void setStyle(double stroke) {
    getController().setStyle(strokeWidth: stroke);
    strokeWidth.value = getController().drawConfig.value.strokeWidth;
  }

  /// 设置 画笔颜色
  void setColor(Color color) {
    getController().setStyle(color: color);
    strokeWidth.value = getController().drawConfig.value.strokeWidth;
  }

  /// 设置绘制内容
  void setPaintContent(IconData iconData) {
    tid.value = iconData;

    if (iconData == Icons.edit) {
      getController().setPaintContent(SimpleLine());
    } else if (iconData == Icons.brush) {
      getController().setPaintContent(SmoothLine());
    } else if (iconData == Icons.show_chart) {
      getController().setPaintContent(StraightLine());
    } else if (iconData == CupertinoIcons.stop) {
      getController().setPaintContent(Rectangle());
    } else if (iconData == CupertinoIcons.circle) {
      getController().setPaintContent(Circle());
    } else if (iconData == CupertinoIcons.bandage) {
      getController().setPaintContent(Eraser(color: Colors.white));
    }
  }

  /// 操作类型枚举
  void setActions(ActionsEnum ae) {
    LogUtils.d('DrawingBoardController setActions $ae');

    if (ae == ActionsEnum.undo) {
      getController().undo();
    } else if (ae == ActionsEnum.redo) {
      getController().redo();
    } else if (ae == ActionsEnum.turn) {
      getController().turn();
    } else if (ae == ActionsEnum.clear) {
      DialogUtils.question(() {
        LogUtils.d('DrawingBoardController setActions cancel');
      }, () {
        LogUtils.d('DrawingBoardController setActions ok');
        getController().clear();
      });
    }
  }

  String json = '';

  /// 获取画板数据
  void dbTest(IconData icon) async {
    if (icon == Icons.data_array) {
      // 通过 DrawingController.getJsonList 获取内容 Json
      json = const JsonEncoder.withIndent('  ')
          .convert(_drawingController.getJsonList());
    } else if (icon == Icons.image) {
      // 通过 DrawingController.getImageData 获取画板数据
      Int8List? il =
          (await _drawingController.getImageData())?.buffer.asInt8List();
    } else if (icon == Icons.add) {
      // 向画板添加绘制内容
      final List<dynamic> tData = jsonDecode(json) as List<dynamic>;
      final List<PaintContent> contents = [];
      for (int i = 0; i < tData.length; i++) {
        final item = tData[i] as Map<String, dynamic>;
        final type = item['type'];
        if (type == ConstantUtils.SimpleLine) {
          contents.add(SimpleLine.fromJson(item));
        } else if (type == ConstantUtils.SmoothLine) {
          contents.add(SmoothLine.fromJson(item));
        } else if (type == ConstantUtils.StraightLine) {
          contents.add(StraightLine.fromJson(item));
        } else if (type == ConstantUtils.Rectangle) {
          contents.add(Rectangle.fromJson(item));
        } else if (type == ConstantUtils.Circle) {
          contents.add(Circle.fromJson(item));
        }
      }
      getController().addContents(contents);
    } else if (icon == Icons.data_exploration) {
      // 数据库添加查询
      _dbQuery();
    } else if (icon == Icons.query_stats) {
      // 数据库添加
      // _dbInsert();
    }
  }

  void _dbQuery() async {
    final list = await SqlUtils.drawContentQuery();
    list?.length;
  }

  void _dbInsert(Map<String, Object?> values) async {
    final result = await SqlUtils.drawContentInsert(values);
    result;
  }

  @override
  void onInit() {
    LogUtils.d('DrawingBoardController onInit');
    super.onInit();
  }

  @override
  void onReady() {
    LogUtils.d('DrawingBoardController onReady');
    super.onReady();
  }

  @override
  void onClose() {
    LogUtils.d('DrawingBoardController onClose');
    super.onClose();
  }
}
